package core.manager.playerManager.musicPlayerManager
{
    import core.manager.scriptManager.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    import memory.*;

    public class BackGroundMusicManager
    {
        private var _hasBackGroundSound:Boolean = true;
        private var soundLoaderContext:SoundLoaderContext;
        private var sound:Sound;
        private var soundChannel:SoundChannel;
        private var currentMusic:String;
        private static var instance:BackGroundMusicManager;

        public function BackGroundMusicManager()
        {
            if (instance == null)
            {
                instance = this;
            }
            else
            {
                throw new Error("不能实例化");
            }
            init();
            return;
        }// end function

        public function soundStop() : void
        {
            if (soundChannel != null)
            {
                soundChannel.stop();
            }
            return;
        }// end function

        private function soundFinished(userEquation:Event) : void
        {
            if (sound != null)
            {
                MemoryRecovery.getInstance().gcFun(soundChannel, Event.SOUND_COMPLETE, soundFinished);
                soundChannel = sound.play(0);
                soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished, false, 0, true);
            }
            return;
        }// end function

        private function initScript() : void
        {
            ScriptManager.getInstance().addApi(ScriptName.LOAD_BACKGROUND_MUSIC, loadBackGroundMusic);
            ScriptManager.getInstance().addApi(ScriptName.DISPOSE_BACKGROUND_MUSIC, dispose);
            return;
        }// end function

        public function set hasBackGroundMusic(userEquation:Boolean) : void
        {
            _hasBackGroundSound = userEquation;
            if (!_hasBackGroundSound)
            {
                dispose();
            }
            else
            {
                reload();
            }
            return;
        }// end function

        private function init() : void
        {
            soundLoaderContext = new SoundLoaderContext(1000, true);
            initScript();
            return;
        }// end function

        public function loadBackGroundMusic(userEquation:String) : void
        {
            currentMusic = userEquation;
            if (userEquation != "" && userEquation!="null" && userEquation != null && _hasBackGroundSound)
            {
                sound = new Sound();
                sound.load(new URLRequest(userEquation), soundLoaderContext);
                soundChannel = sound.play();
                soundChannel.addEventListener(Event.SOUND_COMPLETE, soundFinished, false, 0, true);
            }
            else
            {
                dispose();
            }
            return;
        }// end function

        public function reload() : void
        {
            loadBackGroundMusic(currentMusic);
            return;
        }// end function

        public function dispose() : void
        {
            soundStop();
            MemoryRecovery.getInstance().gcFun(soundChannel, Event.SOUND_COMPLETE, soundFinished);
            soundChannel = null;
            sound = null;
            return;
        }// end function

        public static function getInstance() : BackGroundMusicManager
        {
            if (instance == null)
            {
                return new BackGroundMusicManager;
            }
            return instance;
        }// end function

    }
}
